package com.sanglh.tutorial.common.base;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sanglh.tutorial.R;

public class BaseListAdapter<T> extends BaseAdapter {

	private ArrayList<T> entries;
	private Context context;
	private int resId;

	public BaseListAdapter(Context context, ArrayList<T> entries, int resId) {
		super();
		this.entries = entries;
		this.context = context;
		this.resId = resId;
	}

	public BaseListAdapter(Context context, ArrayList<T> entries) {
		super();
		this.entries = entries;
		this.context = context;
		this.resId = R.layout.row_base;
	}

	@Override
	public int getCount() {
		return entries.size();
	}

	@Override
	public Object getItem(int position) {
		return entries.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Object holder;
		T entry = entries.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(resId, null);
			holder = getHolder();
			((Holder) holder).tvName = (TextView) convertView
					.findViewById(R.id.tv_name);
			findViewId(holder, convertView);
			convertView.setTag(holder);
		} else {
			holder = convertView.getTag();
		}

		if (((Holder) holder).tvName != null) {
			((Holder) holder).tvName.setText(((BaseEntry) entry).name);
		}
		setViewResource(holder, entry);

		return convertView;
	}

	public static class Holder {
		public TextView tvName;
	}

	public Object getHolder() {
		return new Holder();
	}

	public void findViewId(Object holder, View convertView) {

	}

	public void setViewResource(Object holder, T entry) {

	}
}
